using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Toenda.Tools.Data;

namespace zapLoader {
	public partial class SettingsForm : Form {
		private string _path;

		public SettingsForm() {
			InitializeComponent();

			this._path = Application.StartupPath + "\\Settings.xml";

			if(File.Exists(this._path)) {
				XmlProvider xml = new XmlProvider(
					this._path,
					OpenMode.ForReading
				);

				this.txtUsername.Text = xml.ReadTag("zapLoader", "username");
				this.txtPassword.Text = xml.ReadTag("zapLoader", "password");

				//MessageBox.Show(this._path);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e) {
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void btnOk_Click(object sender, EventArgs e) {
			StreamWriter sw = new StreamWriter(
				this._path, 
				false
			);

			StringBuilder sb = new StringBuilder();
			sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
			sb.Append("<zapLoader>\n");
			sb.Append("\t<username>" + this.txtUsername.Text.Trim() + "</username>\n");
			sb.Append("\t<password>" + this.txtPassword.Text.Trim()  + "</password>\n");
			sb.Append("</zapLoader>\n");

			sw.Write(sb.ToString());
			sw.Close();
			sw.Dispose();

			this.DialogResult = DialogResult.OK;
			this.Close();
		}
	}
}